﻿using BLL;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDMUI.SingleBatch
{
    public partial class JumpingStation : Form
    {
        private SingleBatchEntryBLL singleBatchEntryBLL = new SingleBatchEntryBLL();
        public JumpingStation()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (int.TryParse(textBox1.Text, out int batchId))
            {
                // 调用 BLL 层的方法获取批次数据
                Batch batchData = singleBatchEntryBLL.showBatchMeg(batchId);
                if (batchData == null)
                {
                    MessageBox.Show("未找到批次数据");
                    return;
                }

                // 获取总流程id
                int totalProcessId = batchData.TotalProcessId;

                // 获取总工艺流程下的所有工站
                List<Station> stationTotolData = singleBatchEntryBLL.GetStationByTotolId(totalProcessId);
                int totalStationCount = stationTotolData.Count;

                // 获取产品数据
                Product productData = singleBatchEntryBLL.GetProductById(batchData.ProductId);
                if (productData == null)
                {
                    MessageBox.Show("未找到产品数据");
                    return;
                }

                // 检查 flowId 和 stationId 是否为总流程长度加 1
                if (productData.FlowId == totalStationCount + 1 && productData.StationId == totalStationCount + 1)
                {
                    MessageBox.Show("该产品已完成所有流程");
                    return; // 不继续执行后续代码
                }

                // 总工艺流程表
                TotalProcess totalProcessData = singleBatchEntryBLL.GetTotalProcessById(totalProcessId);
                int stationId = totalProcessData.TotalProcessId;
                // 工站表
                Station stationData = singleBatchEntryBLL.GetStationById(stationId);
                // 流程id
                int productFlowId = productData.FlowId;
                // 工站iD
                int productStationId = productData.StationId;
                // 工艺流程表
                ProcessFlow FlowData = singleBatchEntryBLL.GetProcessFlowById(productFlowId);
                // 工站表
                Station StationData = singleBatchEntryBLL.GetStationById(productStationId);

                // 将数据分配给窗体上的控件
                batchType.Text = batchData.BatchType;
                verboseType.Text = "Production";
                quantity.Text = batchData.Quantity.ToString();
                subQuantity.Text = batchData.SubQuantity.ToString();
                unit.Text = batchData.Unit.ToString();
                lockingState.Text = "未锁定";
                productNum.Text = batchData.ProductId.ToString();
                workOrderNum.Text = batchData.WorkOrderId.ToString();
                goodNum.Text = batchData.GoodQuantity.ToString();
                noGoodNum.Text = batchData.NGQuantity.ToString();
                wipStatus.Text = batchData.LockStatus.ToString();
                repairStatus.Text = "NotOnhold";
                processFlow.Text = FlowData.FlowName;
                versionNum.Text = FlowData.ProcessFlowVersion;
                describe.Text = FlowData.Description;
                scrapQty.Text = "0";
                scrapSubQty.Text = "0";
                processStatus.Text = "Wait";
                reworkStatus.Text = "unRework";
                station.Text = StationData.StationName;
                stationVesion.Text = StationData.StationVersion;
                StationDescription.Text = StationData.StationDescription;

                // 根据根据工站表的StationStepNumber来更新工站id和g工艺流程id,StationStepNumber+1
                int productId = batchData.ProductId;
                // 获取当前工站ID
                int currentStationId = singleBatchEntryBLL.GetStationIdByProductId(productId);
                // 通过id查询工站信息
                Station stationdata = singleBatchEntryBLL.GetStationById(currentStationId);
                // 获取总流程id
                int totolId = stationdata.TotalProcessId;
                // 获取当前步骤
                int setup = stationdata.StationStepNumber;
                // 获取总流程id获取所有工站
                stationTotolData = singleBatchEntryBLL.GetStationByTotolId(totolId);

                if (stationTotolData.Count > setup)
                {
                    // 通过总流程id和它的setup去查找工站id
                    // 获取下一个工站的 ID
                    int nextStationId = singleBatchEntryBLL.GetNextStationIdByTotalProcessIdAndStepNumber(totolId, setup);
                    // 获取产品数据
                    Product productDataa = singleBatchEntryBLL.GetProductById(productId);
                    productDataa.StationId = nextStationId;
                    // 下一流程id
                    int flowID = singleBatchEntryBLL.GetFlowIdByStationId(nextStationId);
                    // 下一工站id
                    productDataa.StationId = nextStationId;
                    // 通过id查询工站信息
                    Station nextStation = singleBatchEntryBLL.GetStationById(productDataa.StationId);
                    // 工艺流程表
                    ProcessFlow FlowDataa = singleBatchEntryBLL.GetProcessFlowById(flowID);
                    // 下一工艺
                    textBox4.Text = FlowDataa.FlowName;
                    // 版本
                    textBox2.Text = FlowDataa.ProcessFlowVersion;
                    // 描述
                    textBox3.Text = FlowDataa.Description;
                    // 下一工站
                    textBox30.Text = nextStation.StationName;
                    textBox28.Text = nextStation.StationVersion;
                    textBox29.Text = nextStation.StationDescription;
                }
                else
                {
                    // 如果没有下一站，将文本框显示为空
                    textBox4.Text = string.Empty;
                    textBox2.Text = string.Empty;
                    textBox3.Text = string.Empty;
                    textBox30.Text = string.Empty;
                    textBox28.Text = string.Empty;
                    textBox29.Text = string.Empty;
                    MessageBox.Show("已是最后一步！");
                }
            }
            else
            {
                // 处理批次数格式错误的情况
                MessageBox.Show("请输入有效的批次数");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取产品ID
                string productNumText = productNum.Text.Trim();
                if (!int.TryParse(productNumText, out int productId))
                {
                    MessageBox.Show("请输入有效的数字产品ID");
                    return;
                }

                // 根据根据工站表的StationStepNumber来更新工站id和工艺流程id,StationStepNumber+1
                // 获取当前工站ID
                int currentStationId = singleBatchEntryBLL.GetStationIdByProductId(productId);
                // 通过id查询工站信息
                Station stationdata = singleBatchEntryBLL.GetStationById(currentStationId);
                // 获取总流程id
                int totolId = stationdata.TotalProcessId;
                // 获取当前步骤
                int setup = stationdata.StationStepNumber;
                // 获取总流程id获取所有工站
                List<Station> stationTotolData = singleBatchEntryBLL.GetStationByTotolId(totolId);

                if (setup < stationTotolData.Count)
                {
                    // 更新产品的进出站状态
                    int updateResult = singleBatchEntryBLL.UpdateProductEntryAndExitStatus(productId, "未进站");
                    if (updateResult > 0)
                    {
                        MessageBox.Show("产品进出站状态更新成功！");
                    }
                    else
                    {
                        MessageBox.Show("产品进出站状态更新失败！");
                        return; // 如果状态更新失败，直接返回，不继续后续操作
                    }

                    // 通过总流程id和它的setup去查找工站id
                    // 获取下一个工站的 ID
                    int nextStationId = singleBatchEntryBLL.GetNextStationIdByTotalProcessIdAndStepNumber(totolId, setup);
                    // 获取产品数据
                    Product productData = singleBatchEntryBLL.GetProductById(productId);
                    productData.StationId = nextStationId;
                    // 通过工站id查询工艺流程id
                    int flowID = singleBatchEntryBLL.GetFlowIdByStationId(nextStationId);
                    productData.FlowId = flowID;
                    MessageBox.Show(flowID.ToString());
                    int Result = singleBatchEntryBLL.UpdateProduct(productData);
                    MessageBox.Show(Result.ToString());
                }
                else if (setup == stationTotolData.Count)
                {
                    MessageBox.Show("已是最后一步！");
                    return; // 如果已是最后一步，直接返回，不继续后续操作
                }

                // 获取备注
                string remarks = remark.Text.Trim();

                // 获取用户名
                string userNameText = userName.Text.Trim();
                if (string.IsNullOrEmpty(userNameText))
                {
                    MessageBox.Show("请输入用户名");
                    return;
                }

                // 根据用户名获取用户ID
                int userId = singleBatchEntryBLL.GetUserIdByUsername(userNameText);
                if (userId == -1)
                {
                    MessageBox.Show("未找到用户ID");
                    return;
                }
                // 创建 BatchStation 对象
                BatchStation batchStation = new BatchStation
                {
                    ProductId = productId,
                    //ProcessPackageId = processPackageId,
                    UserId = userId,
                    Remark = remarks,
                };
                BatchStationRepository batchStationRepository = new BatchStationRepository();
                // 调用 DAL 层的方法添加数据
                int result = batchStationRepository.AddBatchStation(batchStation);

                if (result > 0)
                {
                    MessageBox.Show("数据添加成功！");
                }
                else
                {
                    MessageBox.Show("数据添加失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误: {ex.Message}");
            }
        }
    }
}
